//增强基本语法
Date.prototype.format = function(format) {
  var o = {
   "M+" : this.getMonth()+1, //month
   "d+" : this.getDate(),    //day
   "h+" : this.getHours(),   //hour
   "m+" : this.getMinutes(), //minute
   "s+" : this.getSeconds(), //second
   "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
   "S" : this.getMilliseconds() //millisecond
  }
  if(/(y+)/.test(format)) 
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length))
  for(var k in o)
    if(new RegExp("("+ k +")").test(format))
      format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length))
  return format;
}
//判断数组是否包含某个元素
Array.prototype.contains = function(target) {
  for(var i = 0; i < this.length; i++){
    if(target === this[i]){
        return true;
    }
  }
  return false
}

File.prototype.compress = function() {
  let file = this
  return new Promise((res, rej )=> {
    let reader = new FileReader()
    reader.readAsDataURL(file)
    var img = new Image
    reader.onload = function (e) {
      const width = 400, //图像大小
      quality = 0.8, //图像质量
      canvas = document.createElement("canvas"),
      drawer = canvas.getContext("2d")
      img.src = this.result
      img.onload = ()=> {
        canvas.width = width
        canvas.height = width * (img.height / img.width)
        drawer.drawImage(img, 0, 0, canvas.width, canvas.height)
        img.src = canvas.toDataURL("image/png", quality)

        let arr = img.src.split(','), 
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), 
            n = bstr.length, 
            u8arr = new Uint8Array(n)
        while(n--){
          u8arr[n] = bstr.charCodeAt(n)
        }
        let newFile = new Blob([u8arr], file.name, {type:mime})
        res(newFile)
      }
    }
  }) 
}